package com.example.wenmini.controller;


import com.example.wenmini.config.AjaxResult;
import com.example.wenmini.entity.Questionnaire;
import com.example.wenmini.service.ParseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.Future;

@RestController
@RequestMapping("/api/parse")
@Slf4j
public class ParseController {

    @Autowired
    private ParseService parseService;


   @PostMapping
   public Object parseQuestionnaire(@RequestBody Map<String, String> request) {
       String url = request.get("url");
       log.info("收到问卷解析请求，URL：{}", url);

       if (url == null || url.trim().isEmpty()) {
           log.warn("问卷URL为空");
           return AjaxResult.error("问卷URL为空");
       }

       try {
           Future<Questionnaire> info = parseService.parseQuestionnaire(url);
           return AjaxResult.success(info.get());
       } catch (Exception e) {
           log.error("问卷解析失败：", e);
           return AjaxResult.error("问卷解析失败：" + e.getMessage());
       }
   }

}
